export default {
  namespaced: true,
  state: {
    //菜单栏的折叠和展开
    iscollapse: false,
    //面包屑数据
    tabsList: [
      {
        path: "/",
        name: "home",
        label: "首页",
        icon: "s-home",
        url: "Home/Home",
      },
    ],
    //菜单栏数据
    menu: "",
  },
  mutations: {
    //控制菜单栏的折叠和展开
    collapseMune(state) {
      state.iscollapse = !state.iscollapse;
    },
    //更新面包屑数据
    updateMune(state, item) {
      //   console.log(item);
      if (item.name !== state.tabsList.name) {
        let index = state.tabsList.findIndex(
          (item1) => item1.name === item.name
        );
        if (index === -1) {
          state.tabsList.push(item);
        }
      }
    },
    //删除数据
    closetag(state, item) {
      //
      let index = state.tabsList.findIndex((val) => val.name === item.name);
      state.tabsList.splice(index, 1);
    },
    //动态设置菜单栏数据
    setmenu(state, val) {
      state.menu = val;
      //   console.log(state.menu);
      window.sessionStorage.setItem("router", JSON.stringify(val));
    },
    //动态注册路由
    addmenu(state, router) {
      console.log(router);
      if (!window.sessionStorage.getItem("router")) return;
      let rouetrS = JSON.parse(window.sessionStorage.getItem("router"));
      let routerArray = [];
      rouetrS.forEach((item) => {
        if (item.children) {
          item.children = item.children.map((val) => {
            val.component = () => import("../views/" + val.url);
            return val;
          });
          routerArray.push(...item.children);
          //   console.log(routerArray);
        } else {
          item.component = () => import("../views/" + val.url);
          routerArray.push(item);
        }
      });
      // console.log(routerArray);
      routerArray.forEach((item) => {
        router.addRoute("Main", item);
      });
    },
  },
};
